package com.example.base.security.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.base.config.PageData;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.security.domain.SysRoleEntity;
import com.example.base.security.domain.vo.SysRoleVo;
import com.example.base.security.service.SysRoleService;
import com.example.base.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = {"角色管理"})
@RestController
@RequestMapping("sysrole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation("角色添加/更新接口")
    @PostMapping("addorupdate")
    public JSONObject addSysRoleController(@Validated @RequestBody SysRoleVo vo) throws GlobalExcetion {
        sysRoleService.addOrUpdateSysRole(vo);
        return Result.successInfo("添加成功");
    }

    @ApiOperation("角色分页")
    @GetMapping("page")
    public JSONObject page(@RequestParam(value = "page",defaultValue = "1")Integer page,
                           @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                            @RequestParam(value = "keyword",required = false)String keyword){
        IPage<SysRoleEntity> iPage=sysRoleService.page(page,pageSize,keyword);
        PageData pageData=new PageData(iPage.getTotal(),page,pageSize,iPage.getRecords());
        return Result.successInfo(pageData);
    }
}
